package com.company.huisuo.network.interceptor

import com.company.huisuo.network.auth.TokenManager
import okhttp3.Interceptor
import okhttp3.Response

/**
 * Token拦截器
 * 自动为请求添加Authorization头
 */
class TokenInterceptor : Interceptor {
    
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        
        // 检查是否需要添加token（排除登录等不需要认证的接口）
        val url = originalRequest.url.toString()
        if (url.contains("device-api/meta/device/login")) {
            return chain.proceed(originalRequest)
        }
        
        // 获取token并添加到请求头
        val token = TokenManager.getAccessToken()
        val requestBuilder = originalRequest.newBuilder()
        
        if (!token.isNullOrEmpty()) {
            requestBuilder.addHeader("Authorization", "Bearer $token")
        }
        
        return chain.proceed(requestBuilder.build())
    }
}